package com.sairobo.admin.modules.srblive.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sairobo.base.common.entity.Page;
import com.sairobo.base.common.entity.Query;
import com.sairobo.base.common.entity.R;
import com.sairobo.base.common.service.CommonService;
import com.sairobo.base.common.utils.CommonUtils;
import com.sairobo.admin.modules.srblive.entity.LiveXcxCourses;
import com.sairobo.admin.modules.srblive.manager.LiveXcxCoursesManager;
import com.sairobo.admin.modules.srblive.pojo.vo.LiveXcxCoursesVo;
import com.sairobo.admin.modules.srblive.service.LiveXcxCoursesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

/**
 * 
 *
 * @author admin
 * @email sairobo
 * @url www.sairobo.com
 * @date 2018年1月22日 上午11:48:30
 */
@Service("liveXcxCoursesService")
public class LiveXcxCoursesServiceImpl extends CommonService implements LiveXcxCoursesService {

	@Autowired
	private LiveXcxCoursesManager liveXcxCoursesManager;

	@Override
	public Page<LiveXcxCourses> listLiveXcxCourses(Map<String, Object> params) {
		Query query = new Query(params);
		Page<LiveXcxCourses> page = new Page<>(query);
		liveXcxCoursesManager.listLiveXcxCourses(page, query);
		return page;
	}

	@Override
	public R saveLiveXcxCourses(LiveXcxCourses role) {
        role.setCid(UUID.randomUUID().toString().replace("-","").substring(23));
        int count = liveXcxCoursesManager.saveLiveXcxCourses(role);

        //设置cid
		return CommonUtils.msg(count);
	}

	@Override
	public R getLiveXcxCoursesById(Long id) {
        LiveXcxCoursesVo vo = new LiveXcxCoursesVo();
	    LiveXcxCourses liveXcxCourses = liveXcxCoursesManager.getLiveXcxCoursesById(id);
		if(StringUtils.isNotEmpty(liveXcxCourses.getSeriesid())){
            LiveXcxCourses series = liveXcxCoursesManager.getLiveXcxCoursesByCId(liveXcxCourses.getSeriesid());
            vo.setSeries(series);
        }
        vo.setLiveXcxCourses(liveXcxCourses);
		return CommonUtils.msg(vo);
	}

	@Override
	public R updateLiveXcxCourses(LiveXcxCourses liveXcxCourses) {
		int count = liveXcxCoursesManager.updateLiveXcxCourses(liveXcxCourses);
		return CommonUtils.msg(count);
	}

	@Override
	public R batchRemove(Long[] id) {
		int count = liveXcxCoursesManager.batchRemove(id);
		return CommonUtils.msg(id, count);
	}

    @Override
    public R getSeriesList() {
        EntityWrapper<LiveXcxCourses> ew=new EntityWrapper<>();
        ew.eq("series",1);//0: 单节课  1：系列课
        ew.ne("status",3);//0：待上架 1：已上架  2：已下架  3：已删除
        ew.orderBy("id",false);
        return CommonUtils.msg(liveXcxCoursesManager.selectList(ew));
    }

    @Override
    public R setStatus(Long id, Integer status) {
        LiveXcxCourses courses = liveXcxCoursesManager.selectById(id);
        courses.setStatus(status);
        liveXcxCoursesManager.updateLiveXcxCourses(courses);
        return R.ok();
    }
}
